package com.micro.demo.controller;

import java.util.Date;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class Hello {

	@Value("${application.message:Hello World}")
	private String message = "Hello World";

	private int count;

	@ResponseBody
	@RequestMapping("/hello")
	public String hello(String message) {
		System.out.println(message);
		return message;
	}

	
	@GetMapping("/")
	public String welcome2(Map<String, Object> model) {
		model.put("time", new Date());
		model.put("message", this.message);
		return "welcome";
	}
	
	
	@GetMapping("/welcome")
	public String welcome(Map<String, Object> model) {
		model.put("time", new Date());
		model.put("message", this.message);
		return "welcome";
	}

	@RequestMapping("/foo")
	public String foo(Map<String, Object> model) {
		throw new RuntimeException("Foo");
	}

	@RequestMapping("/getCount")
	@ResponseBody
	public String getCount(HttpServletRequest req) {
		Object obj = req.getAttribute("userName");
		count++;
		if (obj == null) {
			System.out.println("当前线程是：" + Thread.currentThread().getName()+";userName:"+obj+";"
					+ "sessionID:"+req.getSession().getId());
		}
		return  count+"";
	}
}
